package com.lixin.smartclassroom.ui.home.tab2.activity

import com.drake.brv.utils.bindingAdapter
import com.drake.channel.receiveEvent
import com.drake.channel.sendEvent
import com.drake.net.utils.scopeDialog
import com.drake.net.utils.scopeNetLife
import com.lixin.smartclassroom.R
import com.lixin.smartclassroom.api.classstudentcangroup
import com.lixin.smartclassroom.api.creategroup
import com.lixin.smartclassroom.api.deletegroupcustomer
import com.lixin.smartclassroom.api.getcustomerdetailbycode
import com.lixin.smartclassroom.api.searchcustomerbyhudong
import com.lixin.smartclassroom.bean.BaseBean
import com.lixin.smartclassroom.bean.DataListBean
import com.lixin.smartclassroom.common.bindStudentDialog
import com.lixin.smartclassroom.databinding.ActivityMakeGroupBinding
import com.lixin.smartclassroom.ui.home.adapter.groupMember
import com.lixinkeji.base.BaseActivity
import com.lixinkeji.extension.getIntentString
import com.lixinkeji.extension.gone
import com.lixinkeji.extension.onClick
import com.lixinkeji.extension.removeAt
import com.lixinkeji.extension.text
import com.lixinkeji.extension.toast
import com.lixinkeji.extension.visible

/**
 * Description 组队
 * Author: lwc
 * Date:2023/12/13 14:41
 */
class MakeGroupAct : BaseActivity<ActivityMakeGroupBinding>() {
    override fun getViewBinding() = ActivityMakeGroupBinding.inflate(layoutInflater)

    override fun topBarHide() = false
    override fun startPadding() = true
    private lateinit var listBean: ArrayList<DataListBean>
    override fun title() = "组队"
    private var taskType = ""
    private var sendType = ""
    private var xiaojieid = ""
    private var groupid = ""
    private var classId = ""
    private var peoplecount = ""//组队人数


    override fun iniView() {
        hideParentSoftKeyborad(R.id.scrollView)

        getCanGroupStudentData()


        taskType = getIntentString("type")
        classId = getIntentString("classId")
        sendType = getIntentString("sendType")
        xiaojieid = getIntentString("xiaojieid")
        peoplecount = getIntentString("peoplecount")
        groupid = getIntentString("groupid")
//        binding.apply {
//            recycler.groupMember().apply {
//                R.id.tvDelete.onClick {
//                    binding.recycler.bindingAdapter.removeAt(modelPosition)
////                    scopeNetLife {
////                        val baseBean = deletegroupcustomer(
////                            groupid,
////                            (models?.get(modelPosition) as DataListBean).id!!
////                        )
////                        if (baseBean.result == "0")
////                            binding.recycler.bindingAdapter.removeAt(modelPosition)
////                        else
////                            toast(baseBean.resultNote.toString())
////                    }
//
//                }
//                R.id.tvAdd.onClick {
//                    when (getModel<DataListBean>(modelPosition).leixing) {
//                        "1" -> setChecked(modelPosition, true)
//                        else -> toast("只能与学生组队")
//                    }
//                }
//
//            }.models = getData()
//        }

        receiveEvent<BaseBean>("addStudent") {
            val dataListBean = DataListBean()
            dataListBean.image = it.image
            dataListBean.name = it.name
            dataListBean.hudongcode = it.hudong
            dataListBean.id = it.id
            dataListBean.leixing = it.leixing
            binding.etHuDongCode.setText("")
            binding.llInvite.gone()
            listBean.add(dataListBean)
            binding.recycler.bindingAdapter.notifyDataSetChanged()
        }
    }

    private fun getCanGroupStudentData() {
        scopeDialog {
            val listBean = classstudentcangroup(classId,xiaojieid)
            binding.recycler.groupMember().apply {
                R.id.tvDelete.onClick {
                    binding.recycler.bindingAdapter.removeAt(modelPosition)
//                    scopeNetLife {
//                        val baseBean = deletegroupcustomer(
//                            groupid,
//                            (models?.get(modelPosition) as DataListBean).id!!
//                        )
//                        if (baseBean.result == "0")
//                            binding.recycler.bindingAdapter.removeAt(modelPosition)
//                        else
//                            toast(baseBean.resultNote.toString())
//                    }

                }
                R.id.tvAdd.onClick {
                    when (getModel<DataListBean>(modelPosition).leixing) {
                        "1" -> setChecked(modelPosition, true)
                        else -> toast("只能与学生组队")
                    }
                }
            }.models = listBean?.dataList
        }
    }

    private fun getData(): ArrayList<DataListBean> {
        listBean = ArrayList<DataListBean>()
        return listBean
    }

    override fun onClick() {
        binding.apply {
            btnSure.onClick {
                if (etGroupName.text().isEmpty()) {
                    toast("请输入小组名称")
                    return@onClick
                }
                if (recycler.bindingAdapter.models == null || recycler.bindingAdapter.getCheckedModels<DataListBean>()
                        .isEmpty()
                ) {
                    toast("请先添加队员")
                    return@onClick
                }

                if (peoplecount.toInt() - 1 == recycler.bindingAdapter.getCheckedModels<DataListBean>()?.size!!) {
                    var ids = ""
                    for (i in 0 until recycler.bindingAdapter.getCheckedModels<DataListBean>().size) {
                        if (i == 0) {
                            ids =
                                (recycler.bindingAdapter.getCheckedModels<DataListBean>()[i]).id.toString()
                        } else {
                            ids += "," + (recycler.bindingAdapter.getCheckedModels<DataListBean>()[i]).id.toString()
                        }
                    }
                    scopeNetLife {
                        val baseBean = creategroup(xiaojieid, etGroupName.text(), ids)
                        sendEvent("cs", "refreshGroupMessage")
                        if (baseBean.result == "0")
                            finish()
                    }
                } else if (peoplecount.toInt() - 1 > recycler.bindingAdapter.getCheckedModels<DataListBean>()?.size!!) {
                    toast("组队人数不足")
                    return@onClick
                } else if (peoplecount.toInt() - 1 < recycler.bindingAdapter.getCheckedModels<DataListBean>()?.size!!) {
                    toast("组队人数已超")
                    return@onClick
                }


            }
            tvAddMember.onClick {
                llInvite.visible()
            }


            tvConfirm.onClick {
                if(etHuDongCode.text().isEmpty()){
                    toast("请输入互动码")
                    return@onClick
                }
                scopeNetLife {
                    val baseBean = searchcustomerbyhudong(etHuDongCode.text(),xiaojieid)
                    if (baseBean.result == "0") {
                        val dataListBean = DataListBean()
                        dataListBean.image = baseBean.image
                        dataListBean.name = baseBean.name
                        dataListBean.hudongcode = baseBean.hudong
                        dataListBean.id = baseBean.id
                        dataListBean.leixing = baseBean.leixing
                        binding.etHuDongCode.setText("")
                        binding.llInvite.gone()
                        listBean.add(dataListBean)
                        binding.recycler.bindingAdapter.notifyDataSetChanged()


//                        when(baseBean.leixing){
//                            "1"->bindStudentDialog("确定与该生组队", 2, "确定", baseBean)
//                            else->toast("只能与学生组队")
//                        }
                    } else {
                        toast(baseBean.resultNote.toString())
                    }

                }

            }
        }
    }

}